﻿using System;
using AuthoritExtensibility;
using WordCount.Properties;

namespace WordCount
{
	/// <summary>
	/// The <see cref="PluginManager"/> class implements the Author-it word count plug-in.
	/// </summary>
	public class PluginManager : IPlugin
	{
		private ToolbarAddArgs bookEditorArgs;
		private Config config;
		private ToolbarAddArgs mainFormArgs;
		private AitApplicationProxy myProxy;

		/// <summary>
		/// The <see cref="PluginManager(AitApplicationProxy)"/> constructor initiates a new instance of the <see cref="PluginManager"/> class./>
		/// </summary>
		/// <param name="proxy">The instance of the <see cref="AitApplicationProxy"/> class that the Author-it application passes to the plug-in.</param>
		public PluginManager(AitApplicationProxy proxy)
		{
			config = new Config();
			myProxy = proxy;
			mainFormArgs = new ToolbarAddArgs();
			bookEditorArgs = new ToolbarAddArgs();
			if (Settings.Default.ToolbarShowButtonImage)
			{
				mainFormArgs.IconImage = Resources.word_count_button;
				bookEditorArgs.IconImage = Resources.word_count_button;
			}
			mainFormArgs.PluginName = Settings.Default.PluginNameMainForm;
			bookEditorArgs.PluginName = Settings.Default.PluginNameBookEditor;
			if (Settings.Default.ToolbarShowButtonName)
			{
				mainFormArgs.TargetButtonName = Resources.ButtonName;
				bookEditorArgs.TargetButtonName = Resources.ButtonName;
			}
			else
			{
				mainFormArgs.TargetButtonName = String.Empty;
				bookEditorArgs.TargetButtonName = String.Empty;
			}
			mainFormArgs.TargetForm = TargetForm.MainForm;
			bookEditorArgs.TargetForm = TargetForm.BookEditor;
			mainFormArgs.TargetGroupName = Settings.Default.TargetGroupNameToolbar;
			bookEditorArgs.TargetGroupName = config.TargetGroupNameBookEditor;

			if (Settings.Default.ToolbarShowButtonTooltip)
			{
				mainFormArgs.Tooltip = Resources.TooltipToolbar;
				bookEditorArgs.Tooltip = Resources.TooltipBookEditor;
			}
			else
			{
				mainFormArgs.Tooltip = String.Empty;
				bookEditorArgs.Tooltip = String.Empty;
			}
			mainFormArgs.OnClick = clickHandler;
			bookEditorArgs.OnClick = clickHandler;
			myProxy.AddPluginToToolbar(mainFormArgs);
			myProxy.AddPluginToToolbar(bookEditorArgs);
		}

		/// <summary>
		/// The <see cref="clickHandler(object, AuthoritEventArgs)"/> method is the <see cref="AuthoritExtensibility.ToolbarClickHandler"/> for the plug-in buttons.
		/// </summary>
		/// <param name="sender">The object that initiated the event</param>
		/// <param name="e">The <see cref="AuthoritEventArgs"/> passed from Author-it</param>
		private void clickHandler(object sender, AuthoritEventArgs e)
		{
			WordCountForm form = new WordCountForm(e, config);
			form.ShowInTaskbar = Settings.Default.FormShowInTaskbar;
			form.Show();
		}
	}
}